package org.jtb.neodroid;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.jtb.neoapi.Neo;

import android.app.Activity;
import android.graphics.Typeface;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NeoAdapter extends ArrayAdapter {
	private Activity mContext;
	private List<Neo> mNeos;
	private LayoutInflater inflater;
	private DecimalFormat distanceFormat = new DecimalFormat("* ##,###,###");
	private DecimalFormat sizeFormat = new DecimalFormat("##,###");

	NeoAdapter(Activity context, List<Neo> neos) {
		super(context, R.layout.neo, neos);

		this.mContext = context;
		this.mNeos = neos;

		this.inflater = context.getLayoutInflater();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Neo neo = mNeos.get(position);
		View view;
		if (convertView == null) {
			view = inflater.inflate(R.layout.neo, null);
		} else {
			view = convertView;
		}

		view.setBackgroundColor(neo.getColor());

		TextView dayText = (TextView) view.findViewById(R.id.day);
		dayText.setText(neo.getDayString());

		TextView monthYearText = (TextView) view.findViewById(R.id.month_year);
		monthYearText.setText(neo.getMonthString() + " " + neo.getYearString());

		Prefs p = new Prefs(mContext);
		TextView diameterText = (TextView) view.findViewById(R.id.diameter);
		TextView diameterUnitsText = (TextView) view
				.findViewById(R.id.diameter_units);
		TextView missDistanceText = (TextView) view
				.findViewById(R.id.miss_distance);
		TextView missDistanceUnitsText = (TextView) view
				.findViewById(R.id.miss_distance_units);

		if (p.getUnits().equals("metric")) {
			diameterText.setText(sizeFormat.format(neo.getDiameterAverage()));
			diameterUnitsText.setText("meters");
			missDistanceText.setText(distanceFormat.format(neo
					.getMissDistanceKm()));
			missDistanceUnitsText.setText("kilometers");
		} else {
			diameterText
					.setText(sizeFormat.format(neo.getDiameterAverageFeet()));
			diameterUnitsText.setText("feet");
			missDistanceText.setText(distanceFormat.format(neo
					.getMissDistanceMiles()));
			missDistanceUnitsText.setText("miles");
		}

		ImageView imageView = (ImageView) view.findViewById(R.id.image);
		imageView.setImageResource(neo.getImageId());

		return view;
	}
}
